package Control_Admin;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import Model.M_Customer;
import Model.M_Pro;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author thuydao
 */
@WebServlet(urlPatterns = {"/C_Pro_Admin"})
public class C_Pro_Admin extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet C_Pro_Admin</title>");            
            out.println("</head>");
            out.println("<body>");
            
            String action = request.getParameter("ProAction_ad");
            if(action.equals("Insert"))
            {
                String tensp = request.getParameter("tensp");
                String nhacc = request.getParameter("nhacc");
                String madmsp= request.getParameter("madmsp");
                String mota = request.getParameter("mota");
                float giavon = Float.parseFloat(request.getParameter("giavon"));
                 
                float giaban = Float.parseFloat(request.getParameter("giaban"));
                int khuyenmai = Integer.parseInt(request.getParameter("khuyenmai"));
                String trangthai = request.getParameter("trangthai");
                String hinhanh = request.getParameter("hinhanh");
                //out.println(giavon+giaban);
                
                //String tensp, String nhacc, String madmsp, String mota, long giavon, long giaban, int khuyenmai, String trangthai, String hinhanh
                M_Pro pro = new M_Pro();
                boolean result = pro.insert_Pro( tensp,  nhacc,  madmsp,  mota,  giavon,  giaban,  khuyenmai,  trangthai,  hinhanh);
                if(result){
                    RequestDispatcher rd = request.getRequestDispatcher("C_Pro_Show_Admin");
                    rd.forward(request, response);
                }
            }
            else if(action.equals("DeleteAll"))
            {
                M_Pro pro = new M_Pro();
                boolean result = pro.delete_tablePro();
                if(result)
                    out.println("<p style='color:red'>Xóa thành công</p>");
               
            }
            else if(action.equals("delete_pro"))
            {
                int masp = Integer.parseInt(request.getParameter("masp"));
                
                
                M_Pro pro = new M_Pro();
                pro.setMasp(masp);
                boolean result= pro.delete_Pro();
                if(result){
                    RequestDispatcher rd = request.getRequestDispatcher("C_Pro_Show_Admin");
                    rd.forward(request, response);
                }
             
            }
            else if(action.equals("update"))
            {//C_Pro_Admin?masp=100&tensp=no&madmsp=HMLD&nhacc=hai+duon&mota=cute&giavon=6.0&giaban=100000.0&khuyenmai=3&trangthai=Con&hinhanh=pro_9.jpg&ProAction_ad=update
                //int masp, String tensp, String nhacc, String madmsp, String mota, float giavon, float giaban, int khuyenmai, String trangthai, String hinhanh
                int masp = Integer.parseInt(request.getParameter("masp"));
                String tensp = request.getParameter("tensp");
                String nhacc = request.getParameter("nhacc");
                String madmsp= request.getParameter("madmsp");
                String mota = request.getParameter("mota");
                float giavon = Float.parseFloat(request.getParameter("giavon"));                 
                float giaban = Float.parseFloat(request.getParameter("giaban"));
                int khuyenmai = Integer.parseInt(request.getParameter("khuyenmai"));
                String trangthai = request.getParameter("trangthai");
                String hinhanh = request.getParameter("hinhanh");
                //out.println(masp+tensp+nhacc+madmsp+mota+giavon+khuyenmai+trangthai+hinhanh);
                M_Pro pro = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                boolean result = pro.update_Pro();
 //               if(result) out.println("update thành công");
 //               else out.println("Update thất bại");
                RequestDispatcher rd = request.getRequestDispatcher("C_Pro_Show_Admin");
                rd.forward(request, response);
            }
            else if(action.equals("Search"))
            {
                String timtheo = request.getParameter("timtheo");
                String txtSearch=request.getParameter("txtSearch");
                //out.println(timtheo + txtSearch);
                M_Pro[] result=null;
                M_Pro pro = new M_Pro();
                if(timtheo.equals("tensp"))                
                    result = pro.searchTensp(txtSearch);
                else if(timtheo.equals("dmsp"))
                    result = pro.searchDMSP(txtSearch);
                else if(timtheo.equals("nhacc"))
                    result = pro.searchNhacc(txtSearch);
                else if(timtheo.equals("mota"))
                    result = pro.searchMota(txtSearch);
                else 
                    result = pro.searchTrangthai(txtSearch);
//                if(result==null) out.println("Ko tim thay");
//                else out.println("tim thay");
                request.setAttribute("INFO_search", result);
                
                RequestDispatcher rd = request.getRequestDispatcher("Search_Pro.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("Sort"))
            {
                String kieusap = request.getParameter("kieusap");
                
                //out.println(timtheo + txtSearch);
                M_Pro[] result=null;
                M_Pro pro = new M_Pro();
                if(kieusap.equals("tang"))                
                    result = pro.sortDESC();
               
                else 
                    result = pro.sortASC();
            
                request.setAttribute("INFOPro_Show", result);                   
                RequestDispatcher rd = request.getRequestDispatcher("Production.jsp");
                rd.forward(request, response);
            }
      
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
